const { sequelize } = require("../db/index")
const { DataTypes, Model } = require("sequelize")
const User = require("./user_model")
const Cleaner = require("./cleaner_model")
const moment = require("moment")

class Order extends Model {
  // static async hasCate(id) {
  //   const cate = await Cate.findOne({ where: { id } })
  //   if (cate === null) {
  //     return false
  //   } else {
  //     return true
  //   }
  // }
}
Order.init(
  {
    id: {
      comment: "ID",
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      allowNull: false
    },
    store_id: {
      comment: "门店ID",
      type: DataTypes.INTEGER,
      primaryKey: false,
      allowNull: false
    },
    related_no: {
      comment: "关联订单id",
      type: DataTypes.STRING,
      primaryKey: false,
      allowNull: true
    },
    order_no: {
      comment: "订单号",
      type: DataTypes.STRING,
      primaryKey: false,
      allowNull: false
    },
    order_type: {
      comment: "订单类型",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    customer_id: {
      comment: "联系人",
      type: DataTypes.STRING,
      allowNull: false
    },
    company_name: {
      comment: "",
      type: DataTypes.STRING,
      allowNull: false
    },
    coupon_price: {
      comment: "优惠金额",
      type: DataTypes.DECIMAL,
      allowNull: false
    },
    freight_price: {
      comment: "运费金额",
      type: DataTypes.DECIMAL,
      allowNull: false
    },
    total_price: {
      comment: "总金额",
      type: DataTypes.DECIMAL,
      allowNull: false
    },
    discount: {
      comment: "折扣",
      type: DataTypes.DECIMAL,
      allowNull: false
    },
    total_num: {
      comment: "商品总数量",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    status: {
      comment: "状态",
      type: DataTypes.STRING,
      allowNull: false
    },
    pay_status: {
      comment: "支付状态",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    pay_time: {
      comment: "支付时间",
      type: DataTypes.DATE,
      allowNull: true
    },
    is_delete: {
      comment: "是否删除",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    remarks: {
      comment: "备注",
      type: DataTypes.STRING,
      allowNull: true
    },
    create_time: {
      comment: "创建时间",
      type: DataTypes.DATE,
      get() {
        return moment(this.getDataValue("create_time")).format(
          "YYYY-MM-DD HH:mm:ss"
        )
      }
    },
    update_time: {
      comment: "更新时间",
      type: DataTypes.STRING,
      get() {
        return moment(this.getDataValue("update_time")).format(
          "YYYY-MM-DD HH:mm:ss"
        )
      }
    }
  },
  {
    sequelize,
    modelName: "sp_store_order",
    freezeTableName: true,
    paranoid: false,
    timestamps: false
  }
)

module.exports = Order
